package com.rpc.example.serial;

import com.rpc.example.constant.SerializeTypeEnum;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Elvis
 * @version 1.0, 2022/11/10
 */
public class SerializeManager {

    private static final ConcurrentHashMap<Byte, ISerial> serializer = new ConcurrentHashMap<>();

    static {
        serializer.put(SerializeTypeEnum.JAVA.getCode(), new JavaSerialize());
        serializer.put(SerializeTypeEnum.JSON.getCode(), new JsonSerialize());
    }

    public static ISerial getSerializer(byte type) {
        ISerial iSerial = serializer.get(type);
        if (iSerial == null) {
            return serializer.get(SerializeTypeEnum.JAVA.getCode());
        }
        return iSerial;
    }
}
